package com.atguigu.jxc.controller;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import javax.xml.transform.Result;
import java.util.List;
import java.util.Map;

/**
 * @description 商品信息Controller
 */
@RestController
@RequestMapping("goods/")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    LogService logService;
    /**
     * 分页查询商品库存信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param codeOrName 商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    //http://localhost:8080/goods/listInventory
    @PostMapping({"/listInventory", "/list"})
    public Map<String,Object> getListInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId){

        Map<String,Object> map = goodsService.getListInventory(page,rows,codeOrName,goodsTypeId);
        Log log = new Log(Log.SELECT_ACTION,"分页查询商品库存信息或分页查询商品信息");
        logService.save(log);
        return map;
    }

    /**
     * TODO 加在上面了
     * 分页查询商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param goodsName 商品名称
     * @param goodsTypeId 商品类别ID
     * @return
     */



    /**
     * 生成商品编码
     * @return
     */
    @RequestMapping("/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        Log log = new Log(Log.SELECT_ACTION,"生成商品编码");
        logService.save(log);
        return goodsService.getCode();

    }

    /**
     * 添加或修改商品信息
     * @param goods 商品信息实体
     * @return
     */
    @PostMapping("/save")
    public ServiceVO saveOrUpdate(Goods goods, @PathParam("goodsId") Integer goodsId){
        goodsService.saveOrUpdate(goods,goodsId);
        Log log = new Log(Log.INSERT_ACTION,"添加或修改商品信息");
        logService.save(log);
        return new ServiceVO(100,"请求成功",null);
    }

    /**
     * 删除商品信息
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("/delete")
    public ServiceVO deleteGoods(Integer goodsId){

        goodsService.deleteGoods(goodsId);
        Log log = new Log(Log.DELETE_ACTION,"删除商品信息");
        logService.save(log);
        return new ServiceVO(100,"请求成功",null);
    }

    /**
     * 分页查询无库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("/getNoInventoryQuantity")
    public Map<String,Object> getNoInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        Map<String,Object> map = goodsService.getNoInventoryQuantity(page,rows,nameOrCode);
        Log log = new Log(Log.SELECT_ACTION,"分页查询无库存商品信息");
        logService.save(log);
        return map;
    }

    /**
     * 分页查询有库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("/getHasInventoryQuantity")
    public Map<String,Object> getInventoryQuantity(Integer page,Integer rows,String nameOrCode){
        Map<String,Object> map = goodsService.getHasInventoryQuantity(page,rows,nameOrCode);
        Log log = new Log(Log.SELECT_ACTION,"分页查询有库存商品信息");
        logService.save(log);
        return map;
    }

    /**
     * 添加商品期初库存
     * @param goodsId 商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice 成本价
     * @return
     */
    @PostMapping("/saveStock")
    public ServiceVO updateInventoryQuantityByGoodsId(@PathParam("goodsId")Integer goodsId,
                                                   Integer inventoryQuantity,
                                                   double purchasingPrice){
        goodsService.updateInventoryQuantityByGoodsId(goodsId,inventoryQuantity,purchasingPrice);
        Log log = new Log(Log.INSERT_ACTION,"添加商品期初库存");
        logService.save(log);
        return new ServiceVO(100,"请求成功",null);
    }

    /**
     * 删除商品库存
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("/deleteStock")
    public ServiceVO deleteStock(Integer goodsId){
        Integer code = goodsService.deleteGoodsByGoodsId(goodsId);
        if (code == 200){
            Log log = new Log(Log.DELETE_ACTION,"删除商品库存");
            logService.save(log);
            return new ServiceVO(code,"有进货和销售单据不能删除",null);

        }else{
            return new ServiceVO(code,"删除成功",null);
        }

    }

    /**
     * 查询库存报警商品信息
     * @return
     */
    @PostMapping("/listAlarm")
    public Map<String,Object> getListAlarm(){

        Map<String,Object> map = goodsService.getListAlarm();
        Log log = new Log(Log.SELECT_ACTION,"查询库存报警商品信息");
        logService.save(log);
        return map;
    }
}
